<!--  -->
<template>
    <div>
        <div class="table-page-search-wrapper">
            <a-form layout="inline">
                <a-row :gutter="48">
                    <a-col :md="8" :sm="24">
                        <a-form-item label="通道名称">
                            <a-input v-model="channelName" allow-clear placeholder="请输入通道名称" />
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="通道标识">
                            <a-input v-model="channelTag" allow-clear placeholder="请输入通道标识" />
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="状态">
                            <a-select v-model="isOn" placeholder="请选择" allowClear @change="getConfig">
                                <a-select-option :value="0">关闭</a-select-option>
                                <a-select-option :value="1">开启</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md="16" :sm="24">
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item >
                            <a-button style="margin-right: 20px" type="primary" @click="getConfig()">查询</a-button>
                            <a-button style="margin-right: 20px" icon="plus" @click="add()">新增</a-button>
                            <!-- <a-button icon="question"  shape="circle" size="small" @click="drawerVisible = true"/> -->
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>
        <a-table rowKey="{(record)=>{return record.id ? record.id+record.channelTag : ''}}" :loading="isload" @change="handleTableChange" :columns="columns" :pagination="{ pageSize: 10, total: total ,showSizeChanger: true,pageSize: size}" :data-source="configList"  size="small" :scroll="{ x: 1500, }">
            <div slot="rate" slot-scope="rate">
                <span>{{rate * 100}}%</span>
            </div>
            <div slot="serviceCharge" slot-scope="serviceCharge,record">
                <span>{{serviceCharge}}{{record.channelName.indexOf('FIL') > -1 ? 'FIL' : record.channelName.indexOf('XCH') > -1 ? 'XCH' : record.channelName.indexOf('CNY') > -1 ? 'CNY' : '元'}}</span>
            </div>
            <div slot="singleMinAmount" slot-scope="singleMinAmount,record">
                <span>{{singleMinAmount}}{{record.channelName.indexOf('FIL') > -1 ? 'FIL' : record.channelName.indexOf('XCH') > -1 ? 'XCH' : record.channelName.indexOf('CNY') > -1 ? 'CNY' : '元'}}</span>
            </div>
            <div slot="singleMaxAmount" slot-scope="singleMaxAmount,record">
                <span>{{singleMaxAmount}}{{record.channelName.indexOf('FIL') > -1 ? 'FIL' : record.channelName.indexOf('XCH') > -1 ? 'XCH' : record.channelName.indexOf('CNY') > -1 ? 'CNY' : '元'}}</span>
            </div>
            <div slot="isOn" slot-scope="isOn">
                <a-switch checked-children="开" un-checked-children="关" disabled :checked="isOn == 1 ? true : false" />
            </div>
            <template slot="operation" slot-scope="text, record">
                <div class="editable-row-operations">
                    <a-button type="link" @click="update(record)">修改</a-button>
                    <a-popconfirm
                        title="是否确认删除当前数据?"
                        ok-text="Yes"
                        cancel-text="No"
                        @confirm="del(record)"
                    >
                        <a-button type="link">删除</a-button>
                    </a-popconfirm>
                </div>
            </template>
        </a-table>

        <a-modal
            title="添加、修改通道配置"
            :visible="visible"
            @ok="handleOk"
            @cancel="visible = false"
            >
            <div>
                <a-form :label-col="{ span: 6 }" :wrapper-col="{ span: 17 }">
                    <a-form-item label="通道名称">
                        <a-input v-model="config.channelName" placeholder="请输入通道名称" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="通道标识">
                        <a-input v-model="config.channelTag" placeholder="请输入通道标识" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="处理器名称">
                        <a-input v-model="config.beanName" placeholder="请输入处理器名称" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="费率">
                        <a-input v-model="config.rate" placeholder="请输入通道费率" allow-clear suffix="%"></a-input>
                    </a-form-item>
                    <a-form-item label="单笔手续费">
                        <a-input v-model="config.serviceCharge" placeholder="请输入单笔手续费" allow-clear suffix="元"></a-input>
                    </a-form-item>
                    <a-form-item label="最小单笔金额">
                        <a-input v-model="config.singleMinAmount" placeholder="请输入最小单笔金额" allow-clear suffix="元"></a-input>
                    </a-form-item>
                    <a-form-item label="最大单笔金额">
                        <a-input v-model="config.singleMaxAmount" placeholder="请输入最大单笔金额" allow-clear suffix="元"></a-input>
                    </a-form-item>
                    <a-form-item label="是否开启">
                        <a-radio-group v-model="config.isOn" >
                            <a-radio :value="0">关闭</a-radio>
                            <a-radio :value="1">开启</a-radio>
                        </a-radio-group>
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>

        <!-- 奖励说明 -->
        <a-drawer
            title="通道配置说明："
            @close="drawerVisible = false"
            placement="right"
            width="640"
            :closable="false"
            :visible="drawerVisible"
            >
            <div>单个品牌没有配置通道信息的话，就会默认取总通道配置</div>
            <a-divider />
            <div>单个品牌如果配置了通道信息后，就会走单独的通道配置</div>
            <a-divider />
            <div>单个品牌其他没有配置的通道，还会继续走默认的通道配置</div>
            <a-divider />
            <div>如果想回溯到总通道配置，需要删除掉品牌配置的通道</div>
        </a-drawer>
    </div>
</template>

<script>
import { queryBrandChannelConfig , updateBrandChannelConfig , updateAllChannelConfig } from '@/api/product';
import { columns } from './table';
export default {
    data () {
        return {
            brandId: this.global.brandId,
            brandName: '盛卓美妆',
            brandList: [],
            isOn: undefined,
            channelTag: '', // 通道标识
            channelName: '', // 通道名称
            configList: [],
            size: 10,
            page: 1,
            total: 0,
            isload: false,
            columns,
            visible: false,
            config: {
                id: '',
                channelName: '',
                channelTag: '',
                beanName: '',
                rate: '',
                serviceCharge: '',
                singleMinAmount: '',
                singleMaxAmount: '',
                isOn: '1'
            },
            drawerVisible: false
        };
    },
    created(){
        this.getConfig()
    },
    methods: {
        getConfig(){
            let _this = this
            const params = {
                'brandId':_this.brandId,
                'channelTag':_this.channelTag,
                'channelName':_this.channelName,
                'isOn':_this.isOn,
                'size':_this.size,
                'page':_this.page - 1,
            }
            queryBrandChannelConfig(params)
            .then(res=>{
                if(res.success){
                    _this.configList = res.data.content
                    _this.total = res.data.totalElements
                    _this.isload = false
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getConfig()
        },
        // 添加
        add(){
            this.config.id = ''
            this.config.channelName = ''
            this.config.brandId = this.brandId
            this.config.channelTag = ''
            this.config.beanName = ''
            this.config.rate = ''
            this.config.serviceCharge = ''
            this.config.singleMinAmount = ''
            this.config.singleMaxAmount = ''
            this.config.isOn = 0
            // this.brandName = this.brandId ? this.brandList.filter((item)=>{return item.id == this.brandId})[0].brandName : '总平台'
            this.visible = true
        },
        // 修改
        update(item){
            this.config = JSON.parse(JSON.stringify(item))
            this.config.rate = this.config.rate * 100
            // this.brandName = this.brandId ? this.brandList.filter((item)=>{return item.id == this.brandId})[0].brandName : '总平台'
            this.visible = true
        },
        // 确定添加、修改
        handleOk(){
            let _this = this
            if(_this.config.channelName == ''){
                this.$message.warning('请输入通道名称');
                return
            }
            if(_this.config.channelTag == ''){
                this.$message.warning('请输入通道标识');
                return
            }
            if(_this.config.rate === ''){
                this.$message.warning('请输入通道费率');
                return
            }
            if(_this.config.serviceCharge === ''){
                this.$message.warning('请输入单笔手续费');
                return
            }
            if(_this.config.singleMinAmount === ''){
                this.$message.warning('请输入单笔最小金额');
                return
            }
            if(_this.config.singleMaxAmount === ''){
                this.$message.warning('请输入单笔最大金额');
                return
            }
            const params = _this.brandId ? {
                'channelTag':_this.config.channelTag,
                'brandId':_this.brandId,
                'rate':_this.config.rate / 100,
                'isOn':_this.config.isOn,
                'serviceCharge':_this.config.serviceCharge,
                'singleMinAmount':_this.config.singleMinAmount,
                'singleMaxAmount':_this.config.singleMaxAmount,
            } : {
                'id':_this.config.id,
                'channelName':_this.config.channelName,
                'brandId':_this.brandId,
                'channelTag':_this.config.channelTag,
                'beanName':_this.config.beanName,
                'rate':_this.config.rate / 100,
                'serviceCharge':_this.config.serviceCharge,
                'singleMinAmount':_this.config.singleMinAmount,
                'singleMaxAmount':_this.config.singleMaxAmount,
                'isOn':_this.config.isOn,
            }
            const apiUrl = _this.brandId ? updateBrandChannelConfig(params) : updateAllChannelConfig(params)
            apiUrl
            .then(res=>{
                if(res.success){
                    _this.visible = false
                    _this.getConfig()
                    _this.$notification['success']({
                        message: '提示',
                        description: _this.config.id ? '修改成功' : '添加成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 删除
        del(item){
            let _this = this
            const params = {
                'id': item.id,
                'brandId':_this.brandId,
                'channelTag':item.channelTag,
                'isDelete':1,
            }
            const apiUrl = _this.brandId ? updateBrandChannelConfig(params) : updateAllChannelConfig(params);
            // const apiUrl = updateAllChannelConfig(params);
            apiUrl
            .then(res=>{
                if(res.success){
                    _this.getConfig()
                    _this.$notification['success']({
                        message: '提示',
                        description: '删除成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
    }
}

</script>
<style scoped>

</style>